package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysLessee;
import com.ruoyi.system.service.ISysLesseeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 租户Controller
 * 
 * @author ruoyi
 * @date 2020-07-22
 */
@Controller
@RequestMapping("/system/lessee")
public class SysLesseeController extends BaseController
{
    private String prefix = "system/lessee";

    @Autowired
    private ISysLesseeService sysLesseeService;

    @RequiresPermissions("system:lessee:view")
    @GetMapping()
    public String lessee()
    {
        return prefix + "/lessee";
    }

    /**
     * 查询租户列表
     */
    @RequiresPermissions("system:lessee:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysLessee sysLessee)
    {
        startPage();
        List<SysLessee> list = sysLesseeService.selectSysLesseeList(sysLessee);
        return getDataTable(list);
    }

    /**
     * 导出租户列表
     */
    @RequiresPermissions("system:lessee:export")
    @Log(title = "租户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysLessee sysLessee)
    {
        List<SysLessee> list = sysLesseeService.selectSysLesseeList(sysLessee);
        ExcelUtil<SysLessee> util = new ExcelUtil<SysLessee>(SysLessee.class);
        return util.exportExcel(list, "lessee");
    }

    /**
     * 新增租户
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存租户
     */
    @RequiresPermissions("system:lessee:add")
    @Log(title = "租户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysLessee sysLessee) {
        sysLessee.setCreateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(sysLesseeService.insertSysLessee(sysLessee));
    }

    /**
     * 修改租户
     */
    @GetMapping("/edit/{lesseeId}")
    public String edit(@PathVariable("lesseeId") Long lesseeId, ModelMap mmap)
    {
        SysLessee sysLessee = sysLesseeService.selectSysLesseeById(lesseeId);
        mmap.put("sysLessee", sysLessee);
        return prefix + "/edit";
    }

    /**
     * 修改保存租户
     */
    @RequiresPermissions("system:lessee:edit")
    @Log(title = "租户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysLessee sysLessee)
    {
        sysLessee.setUpdateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(sysLesseeService.updateSysLessee(sysLessee));
    }

    /**
     * 删除租户
     */
    @RequiresPermissions("system:lessee:remove")
    @Log(title = "租户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysLesseeService.deleteSysLesseeByIds(ids));
    }
}
